package com.sogo;


import android.content.ContentValues;  
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DBAdapter {
	public static final String KEY_ROWID = "id";
	public static final String KEY_USRNAME = "username";
	public static final String KEY_FIRSTNAME = "firstname";
	public static final String KEY_LASTNAME = "lastname";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_PASSWORD = "password";
	private static final String TAG = "DBAdapter";
	
	private static final String DATABASE_NAME = "UserDatabase";
	private static final String DATABASE_TABLE = "UserInfo";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = 
			"create table UserInfo (id integer primary key autoincrement, "
			+ "username text not null, firstname text not null, lastname text not null, " +
			"email text not null, password text not null);";
	
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase database;
	
	public DBAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		public void onCreate(SQLiteDatabase database)
		{
			Log.w(TAG, "Upgrading database from version  to , which will destroy all old data");
			try{
				database.execSQL(DATABASE_CREATE);
			}catch (SQLException e){
				e.printStackTrace();
			}
		}
		
		public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + 
					", which will destroy all old data");
			database.execSQL("DROP TABLE IF EXISTS UserInfo");
			onCreate(database);
		}
	}
		public DBAdapter open() throws SQLException
		{
			database = DBHelper.getWritableDatabase();
			return this;
		}
		
		public void close()
		{
			DBHelper.close();
		}
		
		public long insertUser(String username, String firstname, String lastname, String email, String password)
		{
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_USRNAME, username);
			initialValues.put(KEY_FIRSTNAME, firstname);
			initialValues.put(KEY_LASTNAME, lastname);
			initialValues.put(KEY_EMAIL, email);
			initialValues.put(KEY_PASSWORD, password);
			return database.insert(DATABASE_TABLE, null, initialValues);
		}
		
		public boolean updateUser(long rowid, String username, String firstname, String lastname, String email, String password)
		{
			ContentValues args = new ContentValues();
			args.put(KEY_USRNAME, username);
			args.put(KEY_FIRSTNAME, firstname);
			args.put(KEY_LASTNAME, lastname);
			args.put(KEY_EMAIL, email);
			args.put(KEY_PASSWORD, password);
			return database.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowid, null) > 0;
		}
		
		public Cursor getUser()
		{
			return database.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USRNAME, KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_PASSWORD}, null, null, null, null, null);
		}
		

}
